package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysStore;
import com.ruoyi.system.domain.SysTrainStudent;
import com.ruoyi.system.mapper.SysStoreMapper;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTrainStudentMapper;
import com.ruoyi.system.domain.SysTrainStudent;
import com.ruoyi.system.service.ISysTrainStudentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 实训学员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class SysTrainStudentServiceImpl implements ISysTrainStudentService 
{
    @Autowired
    private SysTrainStudentMapper sysTrainStudentMapper;

    @Autowired
    private SysStoreMapper sysStoreMapper;

    @Autowired
    private ISysConfigService configService;
    /**
     * 查询实训学员
     * 
     * @param studentId 实训学员主键
     * @return 实训学员
     */
    @Override
    public SysTrainStudent selectSysTrainStudentByStudentId(Long studentId)
    {
        return sysTrainStudentMapper.selectSysTrainStudentByStudentId(studentId);
    }

    /**
     * 查询实训学员列表
     * 
     * @param sysTrainStudent 实训学员
     * @return 实训学员
     */
    @DataScope(deptAlias = "s", userAlias = "u")
    @Override
    public List<SysTrainStudent> selectSysTrainStudentList(SysTrainStudent sysTrainStudent)
    {
        return sysTrainStudentMapper.selectSysTrainStudentList(sysTrainStudent);
    }

    /**
     * 查询实训学员管理树
     *
     * @param sysTrainStudent 实训学员信息
     * @return 所有实训学员信息
     */
    @Override
    public List<Ztree> selectTrainStudentTree(SysTrainStudent sysTrainStudent)
    {
        List<SysTrainStudent> storeList = sysTrainStudentMapper.selectSysTrainStudentList(sysTrainStudent);
        List<Ztree> ztrees = initZtree(storeList);
        return ztrees;
    }


    /**
     * 对象转实训学员树
     *
     * @param storeList 实训学员列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysTrainStudent> storeList)
    {
        return initZtree(storeList, null);
    }

    /**
     * 对象转实训学员树
     *
     * @param trainStudentList 实训学员列表
     * @param roleTrainStudentList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysTrainStudent> trainStudentList, List<String> roleTrainStudentList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleTrainStudentList);
        String companyName = configService.selectConfigByKey("sys.config.companyName");
        Long defaultCityId = 10000000L;
        Ztree ztree = new Ztree();
        ztree.setpId(0L);
        ztree.setId(defaultCityId);
        ztree.setName(companyName);
        ztree.setTitle(companyName);
        ztrees.add(ztree);

        HashMap<String, Long> citys = new HashMap<String, Long>();
        List<SysDictData> dictData = DictUtils.getDictCache("sys_city_list");

        Long cityId = defaultCityId;
        for (SysDictData city : dictData) {
            ztree = new Ztree();
            ztree.setpId(defaultCityId);
            cityId = cityId + 1;
            citys.put(city.getDictLabel(), cityId);
            ztree.setId(cityId);
            ztree.setName(city.getDictLabel());
            ztree.setTitle(city.getDictLabel());
            ztrees.add(ztree);
        }
        List<SysStore> storeList = sysStoreMapper.selectSysStoreList(new SysStore());
        for (SysStore store : storeList)
        {
            if (UserConstants.SCHOOL_NORMAL.equals(store.getStatus()))
            {
                ztree = new Ztree();
                ztree.setId(store.getStoreId());
                ztree.setpId(citys.get(store.getCity()));
                ztree.setName(store.getStoreName());
                ztree.setTitle(store.getStoreName());
                if (isCheck)
                {
                    ztree.setChecked(roleTrainStudentList.contains(store.getStoreId() + store.getStoreName()));
                }
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }
    /**
     * 新增实训学员
     * 
     * @param sysTrainStudent 实训学员
     * @return 结果
     */
    @Override
    public int insertSysTrainStudent(SysTrainStudent sysTrainStudent)
    {
        return sysTrainStudentMapper.insertSysTrainStudent(sysTrainStudent);
    }

    /**
     * 修改实训学员
     * 
     * @param sysTrainStudent 实训学员
     * @return 结果
     */
    @Override
    public int updateSysTrainStudent(SysTrainStudent sysTrainStudent)
    {
        return sysTrainStudentMapper.updateSysTrainStudent(sysTrainStudent);
    }

    /**
     * 批量删除实训学员
     * 
     * @param studentIds 需要删除的实训学员主键
     * @return 结果
     */
    @Override
    public int deleteSysTrainStudentByStudentIds(String studentIds)
    {
        return sysTrainStudentMapper.deleteSysTrainStudentByStudentIds(Convert.toStrArray(studentIds));
    }

    /**
     * 删除实训学员信息
     * 
     * @param studentId 实训学员主键
     * @return 结果
     */
    @Override
    public int deleteSysTrainStudentByStudentId(Long studentId)
    {
        return sysTrainStudentMapper.deleteSysTrainStudentByStudentId(studentId);
    }
}
